/**
  ******************************************************************************
  * @file    led.c
  * @author  John Doe <vjkqxz@gmail.com>
  * @version V0.0.1
  * @date    15-July-2017
  * @brief   This file provides all the LED functions.
  * @note    This is an example file that show you the code style of driver.
  ******************************************************************************
  * @attention
  *
  * NO ATTENTION!
  *
  * <h2><center>&copy; COPYRIGHT 2017</center></h2>
  ******************************************************************************
  */

/* Includes ------------------------------------------------------------------*/
#include "led.h"

/**
  * @brief  Initializes the LED.
  * @param  None
  * @retval None
  */
void LED_Init(void)
{
  GPIO_InitTypeDef GPIOConfig;

  RCC_APB2PeriphClockCmd(LED_GPIO_CLK, ENABLE);

  GPIOConfig.GPIO_Pin   = LED_Pin;
  GPIOConfig.GPIO_Speed = GPIO_Speed_50MHz;
  GPIOConfig.GPIO_Mode  = GPIO_Mode_Out_PP;
  GPIO_Init(LED_Port, &GPIOConfig);
}

/**
  * @brief  Turn On LED
  * @param  LEDx: specifies the LED to operate.
  *   This parameter can be one of the following values:
  *     @arg LED: Turn On All LED.
  *     @arg LED1:   Turn On LED 1
  *     @arg LED2:   Turn On LED 2
  * @retval None
  */
void LED_On(LEDType LEDx)
{
  /* Check the parameters */
  assert_param(IS_LED(LEDx));

  switch (LEDx)
  {
    case LED1:
      GPIO_SetBits(LED_Port, LED1_Pin);
      break;
    case LED2:
      GPIO_SetBits(LED_Port, LED2_Pin);
      break;
    case LED:
      GPIO_SetBits(LED_Port, LED_Pin);
      break;
    default:
      break;
  }

}

/**
  * @brief  Turn Off LED
  * @param  LEDx: specifies the LED to operate.
  *   This parameter can be one of the following values:
  *     @arg LED: Turn Off All LED.
  *     @arg LED1:   Turn Off LED 1
  *     @arg LED2:   Turn Off LED 2
  * @retval None
  */
void LED_Off(LEDType LEDx)
{
  /* Check the parameters */
  assert_param(IS_LED(LEDx));

  switch (LEDx)
  {
    case LED1:
      GPIO_ResetBits(LED_Port, LED1_Pin);
      break;
    case LED2:
      GPIO_ResetBits(LED_Port, LED2_Pin);
      break;
    case LED:
      GPIO_ResetBits(LED_Port, LED_Pin);
      break;
    default:
      break;
  }
}

/**
  * @brief  Get LED Status
  * @param  LEDx: specifies the LED to operate.
  *   This parameter can be one of the following values:
  *     @arg LED1:   Get LED 1 Status.
  *     @arg LED2:   Get LED 2 Status.
  * @retval Status of LED
  * - ON: LED is ON
  * - OFF: LED is OFF
  */
LEDStatus LED_Status(LEDType LEDx)
{
  LEDStatus status;
  uint8_t   pin;
  /* Check the parameters */
  assert_param(IS_LED_PIN(LEDx));

  switch (LEDx)
  {
    case LED1:
      pin = GPIO_ReadOutputDataBit(LED_Port, LED1_Pin);
      status = ( pin == ON ) ? ON : OFF;
      break;
    case LED2:
      pin = GPIO_ReadOutputDataBit(LED_Port, LED2_Pin);
      status = ( pin == ON ) ? ON : OFF;
      break;
    default:
      status = OFF;
      break;
  }

  return status;
}

/**
  * @brief  Toggle LED
  * @param  LEDx: specifies the LED to operate.
  *   This parameter can be one of the following values:
  *     @arg LED: Toggle All LED.
  *     @arg LED1:   Toggle LED 1
  *     @arg LED2:   Toggle LED 2
  * @retval None
  */
void LED_Toggle(LEDType LEDx)
{
  uint16_t pins;
  /* Check the parameters */
  assert_param(IS_LED(LEDx));

  pins = GPIO_ReadOutputData(LED_Port);

  switch (LEDx)
  {
    case LED1:
      if ((pins & LED1_Pin) == ((uint16_t)0x0000))
      {
        GPIO_SetBits(LED_Port, LED1_Pin);
      }
      else
      {
        GPIO_ResetBits(LED_Port, LED1_Pin);
      }
      break;
    case LED2:
      if ((pins & LED2_Pin) == ((uint16_t)0x0000))
      {
        GPIO_SetBits(LED_Port, LED2_Pin);
      }
      else
      {
        GPIO_ResetBits(LED_Port, LED2_Pin);
      }
      break;
    case LED:
      LED_Toggle(LED1);
      LED_Toggle(LED2);
      break;
    default:
      break;
  }
}

/*****************************************************************************/
